<!DOCTYPE html>
<html>
	<head>
		<title>发放优惠券</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css & js 资源 -->
		<link rel="stylesheet" href="../../static/kj/element-ui/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css"> 
		<script src="../../static/vue.min.js"></script>
		<script src="../../static/kj/element-ui/index.js"></script>
		<script src="../../static/jquery.min.js"></script>
		<script src="../../static/kj/layer/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<script src="../../sa-resources/router.js"></script>
	</head>
	<body>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<!-- 参数栏 -->
			<div class="c-panel">
				<div class="c-title">发放优惠券</div>
				<el-form v-if="m">
					<div class="c-item br">
						<label class="c-label">优惠券：</label>
						<el-select v-model="m.coupon_id" filterable placeholder="请选择">
							<el-option v-for="item in couponList" :key="item.coupon_id" :label="item.name" :value="item.coupon_id"></el-option>
						</el-select>
					</div>
					<div class="c-item br">
						<label class="c-label">发放张数：</label>
						<el-input size="mini" v-model="m.num" placeholder="每客户发放张数"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">发放时间：</label>
						<el-date-picker v-model="m.assign_at" type="datetime" placeholder="为空表示立即发放"></el-date-picker>
					</div>
					<div class="c-item br">
						<label class="c-label">类型：</label>
						<el-radio v-model="m.type" label="GROUP">客户群组</el-radio>
						<el-radio v-model="m.type" label="CUSTOMER" disabled>指定客户</el-radio>
					</div>
					<div class="c-item br">
						<label class="c-label">客户组：</label>
						<el-select v-model="m.entity_id" filterable placeholder="请选择">
							<el-option v-for="item in groupList" :key="item.group_id" :label="item.group_name" :value="item.group_id"></el-option>
						</el-select>
					</div>
					<div class="c-item br">
						<label class="c-label">备注：</label>
						<el-input size="mini" v-model="m.remark"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label"></label>
						<el-button size="mini" type="primary" icon="el-icon-plus" size="mini" @click="ok">确定</el-button>
					</div>
				</el-form>
			</div>
		</div>
		<script type="text/javascript">
			new Vue({
				el: '.vue-box',
				data: {
					id: sa.p('id', 0),
					m: {
						type: 'GROUP',
						coupon_id: '',
						entity_id: '',
						num: '',
						assign_at: '',
					},
					couponList: [],
					groupList: [],
				},
				methods: {
					// 创建一个 默认Model 
					createModel: function() {
						return {};
					},
					// ok
					ok: function() {
						if(this.submitCheck() != 'ok') {
							return;
						}
						sa.ajax(router.assignCoupon, this.m, function(res){
							sa.alert('操作成功', function(res) {
								sa.closeCurrIframe();	// 关闭本页
							}.bind(this)); 
						}.bind(this));
					},
					// 表单验证
					submitCheck: function() {
						try{
							var m = this.m; // 获取 m对象
							sa.checkNull(m.type, '请选择发放对象类型');
							sa.check(m.coupon_id, '请选择优惠券');
							sa.checkNull(m.entity_id, '请选择发放对象');
							sa.checkNull(m.num, '请输入发放数量');
							return 'ok'; // 全部通过验证，返回ok 表示正确
						} catch(e) {
							sa.error(e);
						}
					},
				},
				mounted: function(){
					// 初始化数据
					sa.ajax(router.getCouponList, {is_active: 1, status: 1, page_size: 99999}, function(res) {
						this.couponList = res.list;
					}.bind(this), {type: 'get'});
					sa.ajax(router.getGroupList, {type: 'CUSTOMER', is_active: 1, page_size: 99999}, function(res) {
						this.groupList = res.list;
					}.bind(this), {type: 'get'});
				}
			})
		</script>
	</body>
</html>